package com.qingge.springboot.controller;


import cn.hutool.core.lang.hash.Hash;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qingge.springboot.service.ISubjectService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qingge.springboot.common.Result;

import com.qingge.springboot.service.IScoreService;
import com.qingge.springboot.entity.Score;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xyc
 * @since 2022-04-15
 */
@RestController
@RequestMapping("/score")
public class ScoreController {

    @Resource
    private IScoreService scoreService;

    @Resource
    private ISubjectService subjectService;

    // 新增或者更新
    @PostMapping
    public Result save(@RequestBody Score score) {
        scoreService.saveOrUpdate(score);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        scoreService.removeById(id);
        return Result.success();
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        scoreService.removeByIds(ids);
        return Result.success();
    }

    @GetMapping
    public Result findAll() {
        return Result.success(scoreService.list());
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(scoreService.getById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        return Result.success(scoreService.page(new Page<>(pageNum, pageSize)));
    }

    @GetMapping("/scores")
    public Result details(@RequestParam Integer uid) {
        Score score = scoreService.details(uid);

        int[] num = new int[18]; // num 存储不为-1的课程分数
        int count = 0;
        if (score.getS1() != -1) {
            num[count++] = score.getS1();
        }
        if (score.getS2() != -1) {
            num[count++] = score.getS2();
        }
        if (score.getS3() != -1) {
            num[count++] = score.getS3();
        }
        if (score.getS4() != -1) {
            num[count++] = score.getS4();
        }
        if (score.getS5() != -1) {
            num[count++] = score.getS5();
        }
        if (score.getS6() != -1) {
            num[count++] = score.getS6();
        }
        if (score.getS7() != -1) {
            num[count++] = score.getS7();
        }
        if (score.getS8() != -1) {
            num[count++] = score.getS8();
        }
        if (score.getS9() != -1) {
            num[count++] = score.getS9();
        }
        if (score.getS10() != -1) {
            num[count++] = score.getS10();
        }
        if (score.getS11() != -1) {
            num[count++] = score.getS11();
        }
        if (score.getS12() != -1) {
            num[count++] = score.getS12();
        }
        if (score.getS13() != -1) {
            num[count++] = score.getS13();
        }
        if (score.getS14() != -1) {
            num[count++] = score.getS14();
        }
        if (score.getS15() != -1) {
            num[count++] = score.getS15();
        }
        if (score.getS16() != -1) {
            num[count++] = score.getS16();
        }
        if (score.getS17() != -1) {
            num[count++] = score.getS17();
        }
        if (score.getS18() != -1) {
            num[count++] = score.getS18();
        }

        int[] newArray = Arrays.copyOfRange(num, 0, count);
        return Result.success(newArray); // 返回课程分数
    }

    @GetMapping("/names")
    public Result num(@RequestParam Integer uid) {

        Score score = scoreService.details(uid);

        int[] num = new int[18]; // num 存储不为-1的课程分数
        for (int i = 0; i < num.length; i++) {
            num[i] = -1;
        }
        int count = 0;
        if (score.getS1() != -1) {
            num[count++] = 1;
        }
        if (score.getS2() != -1) {
            num[count++] = 2;
        }
        if (score.getS3() != -1) {
            num[count++] = 3;
        }
        if (score.getS4() != -1) {
            num[count++] = 4;
        }
        if (score.getS5() != -1) {
            num[count++] = 5;
        }
        if (score.getS6() != -1) {
            num[count++] = 6;
        }
        if (score.getS7() != -1) {
            num[count++] = 7;
        }
        if (score.getS8() != -1) {
            num[count++] = 8;
        }
        if (score.getS9() != -1) {
            num[count++] = 9;
        }
        if (score.getS10() != -1) {
            num[count++] = 10;
        }
        if (score.getS11() != -1) {
            num[count++] = 11;
        }
        if (score.getS12() != -1) {
            num[count++] = 12;
        }
        if (score.getS13() != -1) {
            num[count++] = 13;
        }
        if (score.getS14() != -1) {
            num[count++] = 14;
        }
        if (score.getS15() != -1) {
            num[count++] = 15;
        }
        if (score.getS16() != -1) {
            num[count++] = 16;
        }
        if (score.getS17() != -1) {
            num[count++] = 17;
        }
        if (score.getS18() != -1) {
            num[count++] = 18;
        }

        String[] temp = new String[18];

        for (int i = 0; i < count; i++) {
            temp[i] = subjectService.findSubjectNum(num[i]);
        }
        String[] last = Arrays.copyOfRange(temp, 0, count);
        return Result.success(last);  // 返回课程名称
    }

}

